﻿using Cemit.PolyProto;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Cemit.NetFramework
{
    /// <summary>
    /// 操作帧帧缓冲器
    /// </summary>
    public class OperationFrameBuffer
    {
        Queue<F> queue = new Queue<F>();
        int i = 0;

        public int Remain => queue.Count;

        public void AddFrame(F frame)
        {
            if (frame.Index != i)
            {
                //Debug.LogWarning(
                //    $"帧序号与应得到的序号不同！remove:{frame.Index} local:{i}");
            }
            i++;
            queue.Enqueue(frame);
        }

        /// <summary>
        /// 格式：playerid:prefix:operation
        /// </summary>
        public IEnumerable<string> ReadFrame(out int index)
        {
            if (queue.Count == 0)
            {
                index = -1;
                return null;
            }
            var frame = queue.Dequeue();
            index = frame.Index;
            return frame.Operation;
        }

        /// <summary>
        /// 格式：playerid:prefix:operation
        /// </summary>
        public IEnumerable<string> ReadFrame()
        {
            return ReadFrame(out _);
        }
    }
}